package com.challenge.sewercoverbackend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.challenge.sewercoverbackend.domian.Cover;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 24270
 * @author 25835
 */
@Component
public interface CoverMapper extends BaseMapper<Cover> {


    /**
     * 带参数的sql语句传参方式
     *
     * @param t
     * @return List
     */
    @Select("select * from datas where CH4=${test}")
    List<Cover> getByCH4(@Param("test") int t);

    @Select("select count(*) from ${tablename}")
    int getCount(@Param("tablename") String tablename);

    /**
     * 查询最新一条数据
     *
     * @return Cover
     */
    @Select("select * FROM ${tablename} ORDER BY id DESC LIMIT 0,1")
    Cover getLatestOne(@Param("tablename") String tablename);

    /**
     * 查询最新二十条数据
     *
     * @return List
     */
    @Select("select * FROM datas ORDER BY id DESC LIMIT 0,20")
    List<Cover> getLatestTwenty();

    /**
     * 获取CH4平均浓度
     *
     * @return double
     */
    @Select("SELECT AVG(CH4) AS CH4Average FROM ${tablename}")
    double getAvgCH4(@Param("tablename") String tablename);

    /**
     * 获取CO平均浓度
     *
     * @return double
     */
    @Select("SELECT AVG(CO) AS CH4Average FROM ${tablename}")
    double gerAvgCO(@Param("tablename") String tablename);

    /**
     * 查询指定日期数据
     *
     * @return List
     */
    @Select("select * from datas where time LIKE '${timeSelect}%'")
    List<Cover> getCoverByTime(@Param("timeSelect") String timeSelect);

    /**
     * 查询一段时间内的数据
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return List
     */
    @Select("select * from ${tablename} where time BETWEEN '${beginTime}%' AND '${endTime}%'")
    List<Cover> getCoverByTwoTime(@Param("tablename") String tablename, @Param("beginTime") String beginTime, @Param("endTime") String endTime);

    /**
     * 查询最新n条数据
     *
     * @return List
     */
    @Select("select * FROM ${tablename} ORDER BY id DESC LIMIT ${number}")
    List<Cover> getLatestByNum(@Param("tablename") String tablename, @Param("number") int number);

    /**
     * 查询最新n条CO
     *
     * @return List
     */
    @Select("select CO FROM ${tablename} ORDER BY id DESC LIMIT 0,${number}")
    List<Integer> getCOByNum(@Param("tablename") String tablename, @Param("number") int number);

    /**
     * 查询最新n条CH4
     *
     * @return List
     */
    @Select("select CH4 FROM ${tablename} ORDER BY id DESC LIMIT 0,${number}")
    List<Integer> getCH4ByNum(@Param("tablename") String tablename, @Param("number") int number);

    /**
     * 按设备名查询剩余电量
     * @param tablename
     * @return int 电量
     */
    @Select("select BatteryLevel from ${tablename} ORDER BY id DESC LIMIT 1")
    int getElectricity(@Param("tablename") String tablename);

    /**
     * 传参混合使用,不知道怎么写
     */

    @Select("select BatteryLevel from ${tablename} ORDER BY id DESC LIMIT ${number}")
    int getTestDouble(@Param("tablename") String tablename, @Param("number") int number);
}
